<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备编号" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入设备编号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备分类" prop="equipmentCategory">
        <el-select v-model="queryParams.equipmentCategory" placeholder="请选择设备分类" clearable style="width: 240px">
          <el-option v-for="category in categoryOptions" :key="category.value" :label="category.label"
            :value="category.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationModel">
        <el-input v-model="queryParams.specificationModel" placeholder="请输入规格型号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="使用部门" prop="useDepartmentId">
        <el-select v-model="queryParams.useDepartmentId" placeholder="请选择使用部门" clearable style="width: 240px">
          <el-option v-for="dept in deptOptions" :key="dept.value" :label="dept.label" :value="dept.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="出厂厂家" prop="manufacturer">
        <el-input v-model="queryParams.manufacturer" placeholder="请输入出厂厂家" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备类别" prop="equipmentType">
        <el-select v-model="queryParams.equipmentType" placeholder="请选择设备类别" clearable style="width: 240px">
          <el-option v-for="equipmentType in equipmentTypeOptions" :key="equipmentType.value"
            :label="equipmentType.label" :value="equipmentType.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="equipmentStatus">
        <el-select v-model="queryParams.equipmentStatus" placeholder="请选择设备状态" clearable style="width: 240px">
          <el-option v-for="status in statusOptions" :key="status.value" :label="status.label" :value="status.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:role:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:role:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:role:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:role:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentFacilityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" prop="equipmentName" width="120" />
      <el-table-column label="设备编号" prop="equipmentCode" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="设备分类" prop="equipmentCategory" :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          {{ getCategoryText(scope.row.equipmentCategory) }}
        </template>

      </el-table-column>
      <el-table-column label="所在地区" prop="areaLocation" width="100" />
      <el-table-column label="规格型号" prop="specificationModel" width="100" />
      <el-table-column label="使用部门" prop="dept.deptName" width="100">
        <!-- <template slot-scope="scope">
          {{ getDeptText(scope.row.useDepartmentId) }}
        </template> -->

      </el-table-column>
      <el-table-column label="失效日期" prop="expirationDate" width="100" />
      <el-table-column label="设备类别" prop="equipmentType" width="100">
        <template slot-scope="scope">

          {{ getEquipmentTypeText(scope.row.equipmentType) }}

        </template>
      </el-table-column>
      <el-table-column label="设备状态" prop="equipmentStatus" width="100">
        <template slot-scope="scope">

          {{ getEquipmentStatusText(scope.row.equipmentStatus) }}

        </template>
      </el-table-column>
      <el-table-column label="责任人" prop="responsiblePerson" width="100" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备设施库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="equipmentCode">
              <el-input v-model="form.equipmentCode" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备名称" prop="equipmentName">
              <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备分类" prop="equipmentCategory">
              <el-select v-model="form.equipmentCategory" placeholder="请选择设备分类" clearable>
                <el-option v-for="category in categoryOptions" :key="category.value" :label="category.label"
                  :value="category.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="所在区域" prop="areaLocation">
              <el-input v-model="form.areaLocation" placeholder="请选择设备区域" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="specificationModel">
              <el-input v-model="form.specificationModel" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用部门名称" prop="useDepartmentId">
              <el-select v-model="form.useDepartmentId" placeholder="请选择部门名称">
                <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="责任人" prop="responsiblePerson">
              <el-input v-model="form.responsiblePerson" placeholder="请输入责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放位置" prop="storageLocation">
              <el-input v-model="form.storageLocation" placeholder="请输入存放位置" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出厂厂家" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入出厂厂家" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="出厂日期" prop="manufactureDate">
              <el-date-picker v-model="form.manufactureDate" type="date" placeholder="请输入出厂日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="失效日期" prop="expirationDate">
              <el-date-picker v-model="form.expirationDate" type="date" placeholder="请输入失效日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出厂编号" prop="factoryNumber">
              <el-input v-model="form.factoryNumber" placeholder="请输入出厂编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="使用证号" prop="usageCertificateNumber">
              <el-input v-model="form.usageCertificateNumber" placeholder="请输入使用证号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发证日期">
              <el-date-picker v-model="form.certificateIssueDate" type="date" placeholder="请输入发证日期">
              </el-date-picker>
            </el-form-item>

          </el-col>
          <el-col :span="8">
            <el-form-item label="机构核准证书" prop="institutionApprovalCertificate">
              <el-input v-model="form.institutionApprovalCertificate" placeholder="请输入机构核准证书" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="系统名称" prop="systemName">
              <el-input v-model="form.systemName" placeholder="请输入系统名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="投用日期" prop="commissioningDate">
              <el-date-picker v-model="form.commissioningDate" type="date" placeholder="请输入投用日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检测周期" prop="inspectionCycle">
              <el-input v-model="form.inspectionCycle" placeholder="请输入检测周期" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="最近检测时间" prop="lastInspectionTime">
              <el-date-picker v-model="form.lastInspectionTime" type="date" placeholder="请输入最近检测时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最近检测结论" prop="lastInspectionConclusion">
              <el-input v-model="form.lastInspectionConclusion" placeholder="请输入最近检测结论" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="下次检测时间">
              <el-date-picker v-model="form.nextInspectionTime" type="date" placeholder="请输入下次检测时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="检测预警天数" prop="inspectionWarningDays">
              <el-input v-model="form.inspectionWarningDays" placeholder="请输入检测预警天数" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="失效预警天数" prop="expirationWarningDays">
              <el-input v-model="form.expirationWarningDays" placeholder="请输入失效预警天数" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类别" prop="equipmentType">
              <el-select v-model="form.equipmentType" placeholder="请选择设备类别" clearable>
                <el-option v-for="equipmentType in equipmentTypeOptions" :key="equipmentType.value"
                  :label="equipmentType.label" :value="equipmentType.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="设备状态" prop="equipmentStatus">
              <el-select v-model="form.equipmentStatus" placeholder="请选择设备状态" clearable>
                <el-option v-for="status in statusOptions" :key="status.value" :label="status.label"
                  :value="status.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保管岗位名称" prop="storagePostId">
              <el-select v-model="form.storagePostId" placeholder="请选择保管岗位名称" clearable>
                <el-option v-for="post in postOptions" :key="post.value" :label="post.label" :value="post.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查岗位名称" prop="inspectionPostId">
              <el-select v-model="form.inspectionPostId" placeholder="请选择检查岗位名称" clearable>
                <el-option v-for="post in postOptions" :key="post.value" :label="post.label" :value="post.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        
        <el-tab-pane label="设备属性" name="one">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddAttribute" >添加</el-button>
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleDeleteAttribute" >删除</el-button>
          <el-table
            ref="multipleTable"
            :data="attributesList"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionEquipmentAttributes">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              prop="paramName"
              label="参数名称"
              width="120">
            </el-table-column>
            <el-table-column
              prop="paramValue"
              label="参数值"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              prop="unit"
              label="单位"
              show-overflow-tooltip>
            </el-table-column>
          </el-table>
              
        </el-tab-pane>
        <el-tab-pane label="测点参数" name="two">测点参数</el-tab-pane>
        <el-tab-pane label="相关附件" name="three">相关附件</el-tab-pane>
        <el-tab-pane label="设备检测记录" name="four">设备检测记录</el-tab-pane>
        <el-tab-pane label="设备修理记录" name="five">设备修理记录</el-tab-pane>
      </el-tabs>
    </el-dialog>
    <!-- 添加设备参数弹窗 -->
    <el-dialog :title="attributeTitle" :visible.sync="attributeOpen" width="1000px" append-to-body>
      <el-form ref="attributeForm" :model="attributeForm" label-width="80px"> 
        <el-form-item prop="paramName" label="参数名称">
          <el-input v-model="attributeForm.paramName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item prop="paramValue" label="参数值">
          <el-input v-model="attributeForm.paramValue" placeholder="请输入参数值" />
        </el-form-item>
        <el-form-item prop="unit" label="单位">
          <el-input v-model="attributeForm.unit" placeholder="请输入参数单位" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitAttributeForm">确 定</el-button>
            <el-button @click="cancelAttributeForm">取 消</el-button>
          </div>
    </el-dialog>

  </div>
</template>

<script>
import { list,getById,update,add,del,getAttributesByFacilityId,getAttributeById } from "@/api/system/equipmentFacility";
import { listDept } from "@/api/system/dept";
import {listPost} from "@/api/system/post";
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";

export default {
  name: "Role",
  data() {
    return {
      //设备类别
      equipmentTypeOptions: [
        { value: '1', label: '特种设备' },
        { value: '2', label: '生产设备' },
        { value: '3', label: '仪表' }
      ],
      //部门信息
      deptOptions: [],
      //岗位信息
      postOptions: [],
      //状态信息
      statusOptions: [
        { value: 1, label: "正常" },
        { value: 2, label: "停用" }
      ],
      //设备分类
      categoryOptions: [
        { value: 0, label: "默认" },
        { value: 1, label: "分类1" },
        { value: 2, label: "分类2" }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备设施表格数据
      equipmentFacilityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentCode: undefined,
        equipmentName: undefined,
        equipmentCategory: undefined,
        specificationModel: undefined,
        useDepartmentId: undefined,
        manufacturer: undefined,
        equipmentType: undefined,
        equipmentStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipmentCode: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        equipmentCategory: [
          { required: true, message: "设备分类不能为空", trigger: "blur" }
        ],
        areaLocation: [
          { required: true, message: "所在区域不能为空", trigger: "blur" }
        ],
        responsiblePerson: [
          { required: true, message: "责任人不能为空", trigger: "blur" }
        ],
        equipmentType: [
          { required: true, message: "设备类别不能为空", trigger: "blur" }
        ]
      },
      //子表：设备属性表数据
      attributesList:[],
      //子表：设备属性表单
      attributeForm:{},
      //子表：设备属性表单是否可见
      attributeOpen: false,
      //子表：设备属性表单标题
      attributeTitle: "",
      //子表：设备属性多选框选中列
      attributeIds: [],
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    this.getPost();
  },
  methods: {
    //查询部门列表
    getDeptList() {
      listDept().then(response => {
        // this.deptOptions = response.data;
        for (let i = 0; i < response.data.length; i++) {
          this.deptOptions.push({
            value: response.data[i].deptId,
            label: response.data[i].deptName
          });
        }
      });
    },
    /** 查询设备设施列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
      console.info(response)
        this.equipmentFacilityList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.equipmentFacilityList);
      }
      );
    },
    // 查询岗位信息
    getPost() {
      listPost().then(response => {
        for(let i=0;i<response.rows.length;i++){
          this.postOptions.push({
            label: response.rows[i].postName,
            value: response.rows[i].postId
          });
        }
      });
    },
    // 获取设备类别显示文本
    getEquipmentTypeText(typeCode) {
      console.info('..................'+typeCode)
      // return this.typeMap[typeCode]
      // return this.equipmentTypeOptions.find(item => item.value == typeCode).label
      if(this.equipmentTypeOptions.find(item => item.value == typeCode)!=undefined){
        return this.equipmentTypeOptions.find(item => item.value == typeCode).label
      }else{
        return ''

      }
    },
    //获取设备状态文本
    getEquipmentStatusText(statusCode) {
      if (this.statusOptions.find(item => item.value == statusCode)!=undefined) {
        return this.statusOptions.find(item => item.value == statusCode).label
      }else{
        return ''
      }
      
    },
    //设备分类文本
    getCategoryText(category){
      console.log(this.categoryOptions.find(item=>item.value===category).label)
      return this.categoryOptions.find(item=>item.value===category).label
    },
    //根据部门id获取部门名称并渲染在表格中
    // getDeptText(deptId) {
    //   return this.deptOptions.find(item => item.value == deptId).label
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      
      
        this.form = {
          pageNum: 1,
          pageSize: 10,
          equipmentCode: undefined,
          equipmentName: undefined,
          equipmentCategory: undefined,
          specificationModel: undefined,
          useDepartmentId: undefined,
          manufacturer: undefined,
          equipmentType: undefined,
          equipmentStatus: undefined
        };
      this.resetForm("form");
      this.attributesList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备设施库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0]
      getById(id).then(response => {
        this.form = response.data;
        this.open = true;
      });
      getAttributesByFacilityId(id).then(response => {
        this.attributesList = response.rows;
      });
      this.title = "修改设备设施库";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.attributesList=this.attributesList;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eqIds = row.roleId || this.ids;
      this.$modal.confirm('是否确认删除数据？').then(function () {
        return del(eqIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    //子表：设备属性添加按钮
    handleAddAttribute(){
      this.attributeTitle = "添加设备属性";
      this.attributeOpen = true;
    },
    //子表:设备属性点击确定按钮事件
    submitAttributeForm(){
      this.attributeOpen = false;
      this.attributeForm.equipmentFacilityId=this.form.id
      this.attributesList.push(this.attributeForm);
      this.attributeForm = {};
    },
    //子表：设别属性多选框操作
    handleSelectionEquipmentAttributes(selection){
      this.attributeIds = selection.map(item => item.id);
    },
    //子表:设备属性点击删除按钮事件
    handleDeleteAttribute() {
      //根据attributeIds删除attributesList中的数据
      this.attributesList = this.attributesList.filter(item => !this.attributeIds.includes(item.id));
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>